// pages/lead/leadHelp/leadHelp.js

//let http = require('../../../utils/httpHelper.js');
let wallet = require('../../wallet.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    btnok:true,
    nextFlag:true,
    btnFlag:0,
    cF:false,
    detail:{},
    helpLang:{},
    mndata:[],
    randomData:[],
    addData:[],
    numx:0,
    address1:'',
    targetArrId:[],
    numFlag:true,
    activeId:0
  },
  btnOk:function(e){
    this.setData({
      btnok:false
    })
  },
  addWord(e){
    let id = e.target.id
    var len  = this.data.addData.length
    if (this.data.numx >= this.data.mndata.length) {
      this.data.numx = this.data.mndata.length ;
      //this.data.addData.splice(0,id);
    } else {
      if(len<this.data.numx){
        this.data.numx = len
      }else{
        this.data.numx++
      }
    }
    if(this.data.targetArrId[id] === id+''){//类删除
      var dWord = this.data.randomData[id];
      var $this = this;
    var newData =   this.data.addData.filter((res,index,arr)=>{
          if(res==dWord){
            //console.log(res)
           // console.log(index)
            $this.data.addData.splice(index, 1);
          }
      })
      //console.log(newData)
      this.data.targetArrId[id] = ''
      //this.data.addData.splice(id, 1);
    }else{
      this.data.targetArrId[id] = id + '';
      this.data.addData.splice(len, 0, this.data.mndata[id]);
    }
   // var newArr = [...new Set(this.data.addData)]
    var newArr = this.data.addData;
    //console.log(newArr);
    //console.log(this.data.targetArrId)
    this.setData({
      numx:this.data.numx,
      addData:newArr,
      targetArrId: this.data.targetArrId,
      btnFlag: newArr.length >= this.data.mndata.length?0:2
    })
  },
  restoreAccount(){
    wx.navigateTo({
      url: '../leadRestoreAccount/leadRestoreAccount'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //生成助记词；
    // console.log('秒：' + new Date().getSeconds())
    var hdWallet = wallet.productWallet();
    //console.log('w秒：' + new Date().getSeconds())
    var address1 = wallet.addreETH(hdWallet);
    //console.log('n秒：' + new Date().getSeconds())
    var mnemonic = wallet.mnemonic;
    //console.log('d秒：' + new Date().getSeconds())
    var mnArray = mnemonic.split(' ');
    console.log(mnemonic)
    //console.log(mnArray);
    for(var i = 0;i<mnArray.length;i++){
      this.data.targetArrId.push('')
    } 
    //console.log('f秒：' + new Date().getSeconds())
      this.setData({
        mndata:mnArray,
        address1: address1,
        targetArrId:this.data.targetArrId
      })
  },
  createUser(data, hdWallet, mnemonic2){
    var pswd = wx.getStorageSync('pswd');
  
   // var hdWallet = wallet.productWallet(word);
    var address1 = wallet.addreETH(hdWallet);
    var walletKey = JSON.stringify(hdWallet._hdkey.toJSON());
    walletKey = wallet.encryption(pswd, walletKey);//钱包加密；
   
   var  helpWord = wallet.encryption(pswd, mnemonic2)
    wallet.post('member/create-wallet',data,function(res){
        console.log(res)
        wx.setStorageSync('walletKey', walletKey)
        wx.setStorageSync('addressETH', address1)
        wx.setStorageSync('addressBTC', wallet.addreBTC(hdWallet))
        wx.setStorageSync('helpWord', helpWord)
        wx.reLaunch({
          url: '../../assetPage/assetPage'
        })
      })

  },
  nextok:function(e){
    if(this.data.nextFlag == true){
      this.randomsort(this.data.mndata);
        this.setData({
          nextFlag: false,
          btnFlag:2,
          randomData: this.data.mndata
        })
    }
    if (this.data.btnFlag==0){
      //console.log('jinru秒：' + new Date().getSeconds())
      var lang = wx.getStorageSync('lang');
      wx.showLoading({
        title: lang == "en" ? 'loading...' : '加载中...'
      })
      var mnemonic2 = this.data.addData.join(' ');
      //console.log('mnemonic2秒：' + new Date().getSeconds())
      var hdWallet = wallet.productWallet(mnemonic2)
      //console.log('hdWallet秒：' + new Date().getSeconds())
      var address2 = wallet.addreETH(hdWallet)
      //console.log('addreETH秒：' + new Date().getSeconds())
      var tMess = '0xdeadbeaf'
      var btc_message = '123456'
      var token = wx.getStorageSync('createUser').access_token;
      if(address2 == this.data.address1){
        var trSign = wallet.expressMessage(hdWallet);//eth签名
      
        var addressBtc = wallet.addreBTC(hdWallet);
        var signBtc = wallet.signatureBTCMess(hdWallet);//btc签名
        //console.log('btcSign：' + signBtc)

        this.createUser({ access_token: token, message: tMess, btc_message: btc_message, signature: trSign, btc_addr: addressBtc, eth_addr: address2, btc_signature: signBtc }, hdWallet, mnemonic2)
      }else{
        wx.showToast({
          title: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].wrongOrder : wx.T.locales['en'].wrongOrder,
          icon: 'none',
          duration: 2000
        })
      }
    }
  },
  randomsort(arr){
    var t;
    for (var i = 0; i < arr.length; i++) {
      var rand = parseInt(Math.random() * arr.length);
      t = arr[rand];
      arr[rand] = arr[i];
      arr[i] = t;
    }
    return t;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      //console.log(wx.T);
      this.setData({
        helpLang: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'] : wx.T.locales['en']
      })
      wx.setNavigationBarTitle({
        title: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].copyWord : wx.T.locales['en'].copyWord
      })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})